package com.company.cloud.user.modules.app.controller;

import com.company.cloud.captcha.modules.util.StringUtils;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.app.service.UserAppService;
import com.company.cloud.user.modules.base.service.UserInfoService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.app.controller.req.ThirdUserBindByPasswordParam;
import com.company.cloud.user.modules.app.controller.req.ThirdUserBindByVCodeParam;
import com.company.cloud.user.modules.app.controller.req.ThirdUserUpdateParam;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.app.utils.UserUtils;
import com.company.cloud.user.modules.base.entity.ThirdUserInfo;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.service.ThirdUserInfoService;
import com.company.cloud.user.modules.cache.models.ThirdUserModel;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Login
@RestController
@RequestMapping("/app/user/third")
@Api(tags="User_App_第三方用户服务")
public class UserThirdController {
	
	@Autowired
	private ThirdUserInfoService thirdUserInfoService;
	@Autowired
	private UserLoginService userLoginService;
	@Autowired
	private UserCodeService userCodeService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private UserAppService userAppService;

	@GetMapping("/get")
	@ApiOperation(value="获取第三方用户信息",response = ThirdUserModel.class)
	public BaseResult get() {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		if(userInfo.getThirdId()==null) {
			throw new BusinessException(ErrorUserEnum.User_Third_Not);
		}
		ThirdUserInfo entity = thirdUserInfoService.getById(userInfo.getThirdId());
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		ThirdUserModel data= UserConvertUtils.getThirdUserModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/update")
	@StaffLogc(title = "User_App_第三方用户服务 、第三方用户信息修改", action = "User_App_第三方用户服务、第三方用户信息修改", actionAdo = "User_App_第三方用户服务")
	@ApiOperation(value="第三方用户信息修改",response = ThirdUserModel.class)
	public BaseResult update(@RequestBody ThirdUserUpdateParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		LoginUserInfo loginUserInfo= AuthContextUtils.getUserBySimple(userInfo);
		if(userInfo.getThirdId()==null) {
			throw new BusinessException(ErrorUserEnum.User_Third_Not);
		}
		ThirdUserInfo entity = thirdUserInfoService.getById(userInfo.getThirdId());
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if (StringUtils.isEmpty(loginUserInfo.getNickName())) {
			entity.setNickName(params.getNickName());
		} else {
			entity.setNickName(loginUserInfo.getNickName());
		}
		entity.setSex(params.getSex());
		entity.setProvince(params.getProvince());
		entity.setCity(params.getCity());
		entity.setCountry(params.getCountry());
		entity.setHeadImgUrl(params.getHeadImgUrl());
		entity.addEditParam(userInfo);
		thirdUserInfoService.updateById(entity);
		ThirdUserModel data= UserConvertUtils.getThirdUserModel(entity);

		// 同时修改用户信息
		UserInfo userInfoEntity = userInfoService.getById(userInfo.getId());
		userInfoEntity.setNickName(CheckUtils.isEmpty(userInfoEntity.getNickName()) ? params.getNickName() : userInfoEntity.getNickName());
		userInfoEntity.setHeadImg(CheckUtils.isEmpty(userInfoEntity.getHeadImg()) ? params.getHeadImgUrl() : userInfoEntity.getHeadImg());
		userInfoEntity.setSex(CheckUtils.isEmpty(userInfoEntity.getSex()) ? params.getSex() : userInfoEntity.getSex());
		userInfoEntity.addEditParam(userInfo);
		userInfoEntity.addCreateParam(userInfo);
		userInfoService.updateById(userInfoEntity);

		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/bindByPassword")
	@ApiOperation(value="第三方用户根据用户名和密码绑定用户",response = LoginUserInfo.class)
	public BaseResult bindByPassword(@RequestBody ThirdUserBindByPasswordParam params) {
		ValidatorUtils.validate(params);
		userCodeService.validate(params.getVcodeKey(), params.getVcode(), true);
		String companyNo= AuthContextUtils.getCompanyNo();
		
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		LoginUserInfo loginUserInfo= AuthContextUtils.getUserBySimple(userInfo);
		
		if(userInfo.getThirdId()==null) {
			throw new BusinessException(ErrorUserEnum.User_Third_Not);
		}
		
		ThirdUserInfo entity = thirdUserInfoService.getById(userInfo.getThirdId());
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(entity.getUserId()>0) {
			throw new BusinessException(ErrorUserEnum.User_Third_Bind_EXISTS);
		}
		UserInfo user= userLoginService.loginByUserName(companyNo, params.getUserName(), params.getPassword());
		entity.setUserId(user.getId());
		entity.addEditParam(userInfo);
		thirdUserInfoService.updateById(entity);

		LoginUserInfo data= userLoginService.login(user, entity, userInfo.getToken(), null);
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/bindByMobile")
	@ApiOperation(value="第三方用户根据手机号绑定用户",response = LoginUserInfo.class)
	public BaseResult bindByMobile(@RequestBody ThirdUserBindByVCodeParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		LoginUserInfo loginUserInfo= AuthContextUtils.getUserBySimple(userInfo);
		
		String companyNo= AuthContextUtils.getCompanyNo();
		userCodeService.validate(companyNo,UserMsgSendTypeEnum.Third_Bind_VCode, params.getMobile(), params.getVcode(), true);
		
		if(userInfo.getThirdId()==null) {
			throw new BusinessException(ErrorUserEnum.User_Third_Not);
		}
		ThirdUserInfo entity = thirdUserInfoService.getById(userInfo.getThirdId());
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(entity.getUserId()>0) {
			throw new BusinessException(ErrorUserEnum.User_Third_Bind_EXISTS);
		}
		
		UserInfo user = userLoginService.getUserByMobile(companyNo, params.getMobile());

		entity.setUserId(user.getId());
		entity.addEditParam(userInfo);
		thirdUserInfoService.updateById(entity);
		// 设定C端用户权限(是否绑定手机) 移除
		//userAppService.setDefaultUserRole(userInfo.getCompanyNo(), userInfo.getId(), params.getMobile(), loginUserInfo);
		ThirdUserInfo thirdUserInfo = thirdUserInfoService.getById(userInfo.getThirdId());
		LoginUserInfo data= userLoginService.login(user, entity, userInfo.getToken(), null);
		return BaseResult.ok(data);
	}

}
